import java.io.File;
import java.util.Scanner;
//搜索文件，看哪个文件带有关键词，把匹配的结果获取到
public class mySearch {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要查找的目录：");
        String rootPath = sc.next();
        System.out.println("请输入你要查找的关键词：");
        String searchWord = sc.next();
        File rootFile = new File(rootPath);
        if (!rootFile.isDirectory()) {
            System.out.println("输入的路径非法");
            return;
        }
        searchFile(rootFile,searchWord);
    }
        private static void searchFile(File rootFile,String searchWord) {
            //1、列出当前目录有哪些内容
            File[] files = rootFile.listFiles();
            //2、空目录返回
            if (files == null) {
                System.out.println("该目录为空");
                return;
            }
            //3、遍历目录下的每一个结果，进行判定
            for (File f : files) {
                System.out.println("当前扫描文件为：" + f.getAbsolutePath());
                if (f.isFile()) {
                    String fileName = f.getName();
                    if (fileName.contains(searchWord)) {
                        System.out.println("找到匹配结果：" + f.getAbsolutePath());
                        return;
                    }
                } else if (f.isDirectory()) {
                    searchFile(f,searchWord);
                }
            }
    }
}
